namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using Moq;
    using Xunit;

    public sealed class StatusAttributeFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<StatusAttribute>()
                .DerivesFrom<ActionFilterAttribute>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .IsDecoratedWith<AttributeUsageAttribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new StatusAttribute());
        }

        [Fact]
        public void prop_Code()
        {
            Assert.True(new PropertyExpectations(typeof(StatusAttribute).GetProperty("Code"))
                .IsAutoProperty<HttpStatusCode>(HttpStatusCode.OK)
                .Set(HttpStatusCode.Accepted)
                .Result);
        }

        [Fact]
        public void op_OnActionExecuted_FilterExecutedContextNull()
        {
            Assert.Throws<ArgumentNullException>(() => new StatusAttribute().OnActionExecuted(null));
        }

        [Fact]
        public void op_OnActionExecuted_FilterExecutedContext()
        {
            var mock = new Mock<HttpContextBase>();
            mock.SetupSet(x => x.Response.StatusCode).Callback(expected => Assert.Equal<int>((int)HttpStatusCode.OK, expected)).Verifiable();

            new StatusAttribute().OnActionExecuted(new ActionExecutedContext() { HttpContext = mock.Object });

            mock.VerifyAll();
        }

        [Fact]
        public void op_OnActionExecuted_FilterExecutedContextPreconditionFailed()
        {
            HttpStatusCode code = HttpStatusCode.PreconditionFailed;

            var mock = new Mock<HttpContextBase>();
            mock.SetupSet(x => x.Response.StatusCode).Callback(expected => Assert.Equal<int>((int)code, expected)).Verifiable();

            StatusAttribute obj = new StatusAttribute
            {
                Code = code
            };

            ActionExecutedContext executed = new ActionExecutedContext
            {
                HttpContext = mock.Object
            };

            obj.OnActionExecuted(executed);

            mock.VerifyAll();
        }
    }
}